package com.group.controller;


import java.sql.Timestamp;
import java.time.Year;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.group.dao.CommentDao;
import com.group.dao.MediaDao;
import com.group.dao.UserDao;
import com.group.domain.Comment;
import com.group.domain.Media;
import com.group.domain.User;

/**
 * 书影音界面控制类
 * 
 * @author gsd，lz
 * @version 1.0
 */
@Controller
public class MediaController {
	/**	书影音信息数据表接口	**/
	MediaDao mediaDao = new MediaDao();
	/**	评论信息数据表接口	**/
	CommentDao commentDao = new CommentDao();
	/**	用户信息数据表接口	**/
	UserDao userDao = new UserDao();

	/**
	 * 首页
	 * @param model
	 * @return
	 */
	@RequestMapping("/home")
	public String showHome(Model model) {
		
		
		List<Media> bookList = mediaDao.findMeidaListByType("Book");
		if (bookList.size() > 3) {
			int startIndex1 = bookList.size() - 3;
			List<Media> mediaList1 = bookList.subList(startIndex1, bookList.size());
			model.addAttribute("mediaList1", mediaList1);
		} else {
			List<Media> mediaList1 = null;
			mediaList1 = bookList;
			model.addAttribute("mediaList1", mediaList1);
		}

		List<Media> movieList = mediaDao.findMeidaListByType("Movie");
		if (movieList.size() > 3) {
			int startIndex2 = movieList.size() - 3;
			List<Media> mediaList2 = movieList.subList(startIndex2, movieList.size());
			model.addAttribute("mediaList2", mediaList2);
		} else {
			List<Media> mediaList2 = null;
			mediaList2 = movieList;
			model.addAttribute("mediaList2", mediaList2);
		}

		List<Media> musicList = mediaDao.findMeidaListByType("Music");
		if (musicList.size() > 3) {
			int startIndex3 = musicList.size() - 3;
			List<Media> mediaList3 = musicList.subList(startIndex3, musicList.size());
			model.addAttribute("mediaList3", mediaList3);
		} else {
			List<Media> mediaList3 = null;
			mediaList3 = musicList;
			model.addAttribute("mediaList3", mediaList3);
		}
		return "home/home";
	}

	/**
	 * 书影音详情界面
	 * @param id			mediaID
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String showMediaComment(@PathVariable int id, Model model) {
		Media media = mediaDao.findOneById(id);
		List<Comment> commentList = commentDao.findCommentListByPage(1, 8, media);
		model.addAttribute("media", media);
		model.addAttribute("commentList", commentList);
		return "home/comment";
	}

	/**
	 * 电影界面
	 * 
	 * @param map
	 * @param model
	 * @param page
	 * @return
	 */
	@RequestMapping("/movie")
	public String showMovie(ModelMap map, Model model, @RequestParam(defaultValue = "1") int page) {
		long count = mediaDao.countMeida("Movie");
		map.addAttribute("page", page);
		List<Media> mediaList = mediaDao.findTypeListByPage(page, 8,"Movie");
		model.addAttribute("pageCount", Math.ceil(count / 8.0));
		model.addAttribute("movieList", mediaList);
		return "home/movie";
	}

	/**
	 * 音乐界面
	 * @param map
	 * @param model
	 * @param page
	 * @return
	 */
	@RequestMapping("/music")
	public String showMusic(ModelMap map, Model model, @RequestParam(defaultValue = "1") int page) {
		long count = mediaDao.countMeida("Music");
		map.addAttribute("page", page);
		List<Media> mediaList = mediaDao.findTypeListByPage(page, 8,"Music");
		model.addAttribute("pageCount", Math.ceil(count / 8.0));
		model.addAttribute("musicList", mediaList);
		return "home/music";
	}

	/**
	 * 处理筛选请求按钮
	 * 
	 * @param releaseDate
	 * @param tag
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/filter_post", method = RequestMethod.GET)
	public String showFilterText(@RequestParam(value = "year", defaultValue = "") String year,
			@RequestParam(value = "tag", defaultValue = "") String tag,
			@RequestParam(value = "type", defaultValue = "") String type,
			@RequestParam(defaultValue = "1") int page,  ModelMap map, Model model) {

		if (year.equals("全部")) {
			year = "";
		}

		if (tag.equals("全部")) {
			tag = "";
		}

		if (type.equals("全部")) {
			type = "";
		}
		long count = mediaDao.countFilterMedia(year, tag, type);
		map.addAttribute("page", page);
		List<Media> mediaList = mediaDao.findFilterMediaByPage(year, tag, type,page, 8);
		model.addAttribute("pageCount", Math.ceil(count / 8.0));
		
		model.addAttribute("mediaList", mediaList);

		return "home/filter";
	}

	/**
	 * 处理搜索框
	 * 
	 * @param title
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/search", method = RequestMethod.POST)
	public String showSearch(@RequestParam(value = "title", defaultValue = "") String title, Model model) {
		List<Media> mediaList = mediaDao.findBySimilarTitle(title);
		model.addAttribute("mediaList", mediaList);
		return "home/filter";
	}

	/**
	 * 评论提交
	 * 
	 * @param title
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/comment_post", method = RequestMethod.POST)
	public String comementPo(@RequestParam(value = "rating", defaultValue = "") String rating,
													@RequestParam(value = "text", defaultValue = "") String text, 
													@RequestParam(value = "userId", defaultValue = "") String userId, 
													@RequestParam(value = "mediaId", defaultValue = "") String mediaId, Model model) {
		Comment comment = new Comment();
		comment.setCommentDate(new Timestamp(System.currentTimeMillis()));
		comment.setCommentText(text);
		comment.setRating(Integer.parseInt(rating));
		comment.setMedia(mediaDao.findOneById(Integer.parseInt(mediaId)));
		if ("".equals(userId)) {
			return  "redirect:user";
		}
		comment.setUser(userDao.findOneById(Integer.parseInt(userId)));
		commentDao.saveComment(comment);
		return "redirect:" + mediaId;
	}

	/**
	 * 书籍页面
	 * @param map
	 * @param model
	 * @param page
	 * @return
	 */
	@RequestMapping("/book")
	public String showBook(ModelMap map, Model model, @RequestParam(defaultValue = "1") int page) {
		long count = mediaDao.countMeida("Book");
		map.addAttribute("page", page);
		List<Media> mediaList = mediaDao.findTypeListByPage(page, 8,"Book");
		model.addAttribute("pageCount", Math.ceil(count / 8.0));
		model.addAttribute("bookList", mediaList);
		return "home/book";
	}

	/**
	 * 筛选（查询界面）
	 * @param map
	 * @param model
	 * @param page
	 * @return
	 */
	@RequestMapping("/filter")
	public String mediaList(ModelMap map, Model model, @RequestParam(defaultValue = "1") int page) {
		long count = mediaDao.count();
		map.addAttribute("page", page);
		List<Media> mediaList = mediaDao.findMediaListByPage(page, 8);
		model.addAttribute("pageCount", Math.ceil(count / 8.0));
		model.addAttribute("mediaList", mediaList);
		return "home/filter";
	}
	
	
}
